package tlcn.app;

import java.util.ArrayList;
import java.util.List;
import tlcn.business.ManagerData;
import tlcn.data.Material;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

public class SerachActivity extends Activity{

	ListView listView;
	ManagerData managerData;
	ArrayAdapter<String> adapter;
	List<String> listAdapter;
	ArrayList<Material> materials;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_material);
		managerData= new ManagerData(this);
		listView=(ListView) findViewById(R.id.listViewMaterial);
		listAdapter= new ArrayList<String>();
		adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listAdapter);
		listView.setAdapter(adapter);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.search_menu, menu);
		MenuItem item = menu.findItem(R.id.search); 
	    SearchView searchView = (SearchView) item.getActionView();
	    searchView.setOnQueryTextListener(new OnQueryTextListener() {
			@Override
			public boolean onQueryTextSubmit(String query) {
				// TODO Auto-generated method stub
				return false;
			}
			@Override
			public boolean onQueryTextChange(String name) {
				materials=managerData.getMaterialByName(name);
				listAdapter.clear();
				for (Material m : materials) {
					listAdapter.add(m.getMaterialID()+" - "+m.getMaterialName()+" - "+m.getUnit());
				}
				adapter.notifyDataSetChanged();
				return true;
			}
		});
	    return true;
	}

}
